package com.boot.impl;

import com.boot.config.ElasticSearchConfig;
import com.boot.document.WeUserCustomer;
import com.boot.entity.PageInfo;
import com.boot.service.QueryService;
import com.boot.utils.ElasticSearchUtils;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * DESC:
 *
 * @author WangJie
 * @date Created in 2025-03-31 11:06
 */
@Slf4j
@Service
public class QueryServiceImpl implements QueryService {

    @Autowired
    private ElasticSearchConfig elasticSearchConfig;

    @Override
    public void insertDocument() throws Exception {
        WeUserCustomer weUserCustomer = new WeUserCustomer();
        weUserCustomer.setId("user1:customer1");
        weUserCustomer.setUserId("user1");
        weUserCustomer.setUserName("user1");
        weUserCustomer.setCustomerId("customer1");
        weUserCustomer.setCustomerName("customer1");
        weUserCustomer.setGender(1);
        weUserCustomer.setAge(18);
        weUserCustomer.setEmail("email@163.com");
        weUserCustomer.setMobile("13900148916");
        weUserCustomer.setCreateTime("2025-03-31 11:06:00");

        // 创建ES客户端
        RestHighLevelClient restHighLevelClient = elasticSearchConfig.restHighLevelClient();
        ElasticSearchUtils.insertData("we_user_customer", weUserCustomer.getId(), weUserCustomer, restHighLevelClient);
    }

    @Override
    public void updateDocument() throws Exception {
        WeUserCustomer weUserCustomer = new WeUserCustomer();
        weUserCustomer.setId("user1:customer1");
        weUserCustomer.setUserId("user1");
        weUserCustomer.setUserName("user1");
        weUserCustomer.setCustomerId("customer1");
        weUserCustomer.setCustomerName("customer1");
        weUserCustomer.setGender(2);
        weUserCustomer.setAge(25);
        weUserCustomer.setEmail("email@qq.com");
        weUserCustomer.setMobile("13891578916");
        weUserCustomer.setCreateTime("2025-04-11 11:06:00");

        // 创建ES客户端
        RestHighLevelClient restHighLevelClient = elasticSearchConfig.restHighLevelClient();
        ElasticSearchUtils.updateData("we_user_customer", weUserCustomer.getId(), weUserCustomer, restHighLevelClient);
    }

    @Override
    public void insertOrUpdateDocument() throws Exception {
        for (int i = 1; i <= 15; i++) {
            WeUserCustomer weUserCustomer = new WeUserCustomer();
            weUserCustomer.setId("user1:customer" + i);
            weUserCustomer.setUserId("user1");
            weUserCustomer.setUserName("user1");
            weUserCustomer.setCustomerId("customer" + i);
            weUserCustomer.setCustomerName("customer" + i);
            weUserCustomer.setGender(1);
            weUserCustomer.setAge(18);
            weUserCustomer.setEmail("email"+ i +"@163.com");
            weUserCustomer.setMobile("139001489" + i);
            weUserCustomer.setCreateTime("2025-03-31 11:06:00");
            // 创建ES客户端
            RestHighLevelClient restHighLevelClient = elasticSearchConfig.restHighLevelClient();
            ElasticSearchUtils.insertOrUpdateData("we_user_customer", weUserCustomer.getId(), weUserCustomer, restHighLevelClient);
        }
    }

    @Override
    public void deleteDocument() throws Exception {
        // 创建ES客户端
        RestHighLevelClient restHighLevelClient = elasticSearchConfig.restHighLevelClient();
        ElasticSearchUtils.deleteData("we_user_customer", "user1:customer1", restHighLevelClient);
    }

    @Override
    public List<WeUserCustomer> queryWeCustomer(WeUserCustomer weUserCustomer) throws Exception {
        // 创建ES客户端
        RestHighLevelClient restHighLevelClient = elasticSearchConfig.restHighLevelClient();
        // 配置查询条件
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("we_user_customer");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchRequest.source(searchSourceBuilder);
        return ElasticSearchUtils.searchData(searchRequest, WeUserCustomer.class, restHighLevelClient);
    }

    @Override
    public PageInfo<WeUserCustomer> queryWeCustomerPage(WeUserCustomer weUserCustomer) throws Exception {
        // 创建ES客户端
        RestHighLevelClient restHighLevelClient = elasticSearchConfig.restHighLevelClient();
        // 配置查询条件
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("we_user_customer");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.from(0);
        searchSourceBuilder.size(10);
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchRequest.source(searchSourceBuilder);
        return ElasticSearchUtils.searchDataByPage(searchRequest, WeUserCustomer.class, restHighLevelClient);
    }
}
